package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Org;
import com.ruoyi.system.mapper.OrgMapper;
import com.ruoyi.system.service.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 组织机构 服务层实现
 * 
 * @author ruoyi
 * @date 2020-02-12
 */
@Service
public class OrgServiceImpl implements IOrgService
{
	@Autowired
	private OrgMapper orgMapper;

	/**
     * 查询组织机构信息
     * 
     * @param id 组织机构ID
     * @return 组织机构信息
     */
    @Override
	public Org selectOrgById(Long id)
	{
	    return orgMapper.selectOrgById(id);
	}
	
	/**
     * 查询组织机构列表
     * 
     * @param org 组织机构信息
     * @return 组织机构集合
     */
	@Override
	public List<Org> selectOrgList(Org org)
	{
	    return orgMapper.selectOrgList(org);
	}

	@Override
	public List<Ztree> selectOrgTree(Org org){
		List<Org> list = orgMapper.selectOrgList(new Org());
		List<Org> list1= getOrgChildPerms(list,"03");
		List<Ztree> ztrees = initZtree(list1);
		return ztrees;
	}

	public List<Org> getOrgChildPerms(List<Org> list, String parentCode){
		List<Org> returnList = new ArrayList<Org>();
		for (Org org1:list) {
			if (org1.getCode().equals(parentCode)){
				returnList.add(org1);
				break;
			}
		}
		diguiList(list,returnList,parentCode);
		return returnList;
	}

	public void diguiList(List<Org> list,List<Org> returnList, String parentCode){
		List<Org> tempList = getOrgList(list,parentCode);
		if (tempList.size()>0){
			returnList.addAll(tempList);
			for (Org org:tempList) {
				diguiList(list,returnList,org.getCode());
			}
		}
	}

	public List<Org> getOrgList(List<Org> list,String parentCode){

		List<Org> currentList = new ArrayList<>();
		for (Org org:list) {
			if (org.getParentCode().equals(parentCode)){
				currentList.add(org);
			}
		}
		return currentList;
	}
	/**
	 * 对象转部门树
	 *
	 * @param orgList 部门列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<Org> orgList)
	{

		List<Ztree> ztrees = new ArrayList<Ztree>();
		for (Org org : orgList)
		{
			Ztree ztree = new Ztree();
			ztree.setId(org.getId());
			ztree.setpId(getParentId(orgList,org.getParentCode()));
			ztree.setName(org.getName());
			ztree.setTitle(org.getName());

			ztrees.add(ztree);
		}
		return ztrees;
	}
	private Long getParentId(List<Org> orgList,String parentCode){
		for (Org org : orgList)
		{
			if (org.getCode().equals(parentCode)){
				return org.getId();
			}
		}
		return 1L;
	}
    /**
     * 新增组织机构
     * 
     * @param org 组织机构信息
     * @return 结果
     */
	@Override
	public int insertOrg(Org org)
	{
	    return orgMapper.insertOrg(org);
	}
	
	/**
     * 修改组织机构
     * 
     * @param org 组织机构信息
     * @return 结果
     */
	@Override
	public int updateOrg(Org org)
	{
	    return orgMapper.updateOrg(org);
	}

	/**
     * 删除组织机构对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteOrgByIds(String ids)
	{
		return orgMapper.deleteOrgByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<Org> selectOrgListByParentCode(String parentCode){

		List<Org> list = orgMapper.selectOrgList(new Org());
		return getOrgChildPerms(list,parentCode);
	}
	/**
	 * 根据父节点的ID获取所有子节点
	 *
	 * @param list 分类表
	 * @param parentcode 传入的父节点code
	 * @return String
	 */
	public List<Org> getChildPerms(List<Org> list, String parentcode)
	{
		List<Org> returnList = new ArrayList<Org>();
		for (Org org1:list) {
			if (org1.getCode().equals(parentcode)){
				returnList.add(org1);
				break;
			}
		}
		for (Iterator<Org> iterator = list.iterator(); iterator.hasNext();)
		{
			Org t = (Org) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getParentCode().equals(parentcode))
			{
				recursionFn(list, t,returnList);
				returnList.add(t);
			}
		}
		return returnList;
	}

	/**
	 * 递归列表
	 *
	 * @param list
	 * @param t
	 */
	private void recursionFn(List<Org> list, Org t,List<Org> resultList)
	{
		// 得到子节点列表
		List<Org> childList = getChildList(list, t);
		//t.setChildren(childList);
		resultList.addAll(childList);
		for (Org tChild : childList)
		{
			if (hasChild(list, tChild))
			{
				// 判断是否有子节点
				Iterator<Org> it = childList.iterator();
				while (it.hasNext())
				{
					Org n = (Org) it.next();
					recursionFn(list, n,resultList);
				}
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<Org> getChildList(List<Org> list, Org t)
	{
		List<Org> tlist = new ArrayList<Org>();
		Iterator<Org> it = list.iterator();
		while (it.hasNext())
		{
			Org n = (Org) it.next();
			if (n.getParentCode() == t.getCode())
			{
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<Org> list, Org t)
	{
		return getChildList(list, t).size() > 0 ? true : false;
	}
}
